package com.framework.modules.appdiscover;

import com.framework.entity.or.OrBanner;
import com.framework.entity.or.OrBannerType;
import com.framework.entity.sys.SysDeptJumpUrl;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.SysDeptJumpUrlService;
import com.framework.service.app.BannerTypeService;
import com.framework.service.app.DiscoverImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 */
@RequestMapping("sys/discoverImg")
@RestController
public class AppDiscoverImgController extends AbstractController {

    @Autowired
    private DiscoverImgService discoverImgService;

    @Autowired
    private BannerTypeService bannerTypeService;

    @Autowired
    private SysDeptJumpUrlService sysDeptJumpUrlService;


    /**
     * 列表
     * @param params
     * @return
     */
    @RequestMapping("list")
    public RespResult list(@RequestParam Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page=  discoverImgService.queryPage(params);
      return RespResult.ok().put("page",page);
    }

    /**
     * 通过id获取
     * @param id
     * @return
     */
    @RequestMapping("getById/{id}")
    public RespResult getById(@PathVariable Integer id){
        OrBanner orBanner = discoverImgService.selectById(id);
        return RespResult.ok().put("img",orBanner);
    }

    /**
     * 添加
     * @param img
     * @return
     */
    @RequestMapping("addDiscoverImg")
    public  RespResult addImg(@RequestBody OrBanner img){
        img.setCreateDate(new Date());
        img.setDeptId(getDeptId());
        Integer sort =discoverImgService.querySort(img.getType(),getDeptId());
        img.setSort(sort);

        if(img.getJumpType().intValue()==2){
            img.setJumpUrl(sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),1).getBannerJump());
        }
        discoverImgService.insert(img);
        return RespResult.ok();
    }

    /**
     * 修改图片
     * @param img
     * @return
     */
    @RequestMapping("updateImg")
    public RespResult updateImg(@RequestBody OrBanner img){
        if(img.getJumpType().intValue()==2){
            img.setJumpUrl(sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),1).getBannerJump());
        }
        discoverImgService.updateById(img);
        return RespResult.ok();
    }

    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    @RequestMapping("uploadImg")
    public RespResult uploadImg(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),3);
        String prefix=sysDeptJumpUrl.getUploadUrl();
        String suffix;
        String imgUrl;
        suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        imgUrl = OSSFactory.build().uploadSuffix(multipartFile.getBytes(), suffix, prefix);
        return RespResult.ok().put("url",imgUrl);
    }

    @RequestMapping("getTypes/{pType}")
    public RespResult getTypes(@PathVariable Integer pType){
        List<OrBannerType> bannerTypes = bannerTypeService.queryListByParentType(pType);
        return RespResult.ok().put("types",bannerTypes);

    }


}

